/**
 * The database package of the application.
 */
package fr.umlv.m2.android.quizz.database;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteDatabase;

/**
 * This class defines the database theme object.
 * 
 * @author Gambouele Bertrand.
 * @author Viravout Alexandre.
 */
public class ThemeDb {
	/**
	 * The database version.
	 */
	private static final int DB_VERSION = 1;
	/**
	 * The database name.
	 */
	private static final String DB_NAME = "quizz.db";

	/**
	 * The theme table name.
	 */
	private static final String THEME_TABLE = "theme";
	/**
	 * The theme id column name.
	 */
	private static final String ID = "id";
	/**
	 * The theme id column number.
	 */
	private static final int ID_COL_NUM = 0;
	/**
	 * The theme name column name.
	 */
	private static final String NAME = "name";
	/**
	 * The theme name column number.
	 */
	private static final int NAME_COL_NUM = 1;
	/**
	 * The theme level id column name.
	 */
	private static final String LEVEL_ID = "levelId";
	/**
	 * The theme level id column number.
	 */
	private static final int LEVEL_COL_NUM = 2;

	/**
	 * The database of the application.
	 */
	private SQLiteDatabase db;
	/**
	 * The database open helper.
	 */
	private final QuizzDatabaseOpenHelper quizzSQLite;
	
	/**
	 * Create the database theme object.
	 *  
	 * @param context the context.
	 */
	public ThemeDb(Context context) {
		this.quizzSQLite = new QuizzDatabaseOpenHelper(context, DB_NAME, null, DB_VERSION);
	}

	/**
	 * Opens the database.
	 */
	public void open() {
		this.db = this.quizzSQLite.getWritableDatabase();
	}

	/**
	 * Closes the database.
	 */
	public void close() {
		this.db.close();
	}

	/**
	 * Gets the database.
	 * 
	 * @return the database.
	 */
	public SQLiteDatabase getDb() {
		return this.db;
	}
	
	/**
	 * Gets the number of theme.
	 * 
	 * @return the number of theme.
	 */
	public long getNbTheme() {
		return DatabaseUtils.queryNumEntries(this.db, THEME_TABLE);
	}
	
	/**
	 * Gets the number of theme according a level id.
	 * 
	 * @param levelId the level id.
	 * @return the number of theme which match with the level id.
	 */
	public long getNbThemeWithLevel(long levelId) {
		Cursor cursor = this.db.query(
				THEME_TABLE,
				new String[] { ID },
				LEVEL_ID + " = " + levelId,
				null, null, null, null
		);
		int count = cursor.getCount();
		cursor.close();
		
		return count;
	}
	
	/**
	 * Clear the theme table.
	 */
	public void clear() {
		this.quizzSQLite.clearTable(this.db, THEME_TABLE);
	}

	/**
	 * Inserts a theme in the database.
	 * 
	 * @param theme the theme to insert.
	 * @return the row ID of the newly inserted row, or -1 if an error occurred.
	 */
	public long insertLevel(Theme theme) {
		ContentValues values = new ContentValues();

		values.put(NAME, theme.getName());
		values.put(LEVEL_ID, theme.getLevelId());

		return this.db.insert(THEME_TABLE, null, values);
	}

	private List<Theme> cursorToThemes(Cursor cursor) {		
		if(cursor.getCount() == 0) {
			return null;
		} else {
			cursor.moveToFirst();
		}
		
		Theme theme;
		List<Theme> themes = new ArrayList<Theme>();
		do {
			theme = new Theme();
			theme.setId(cursor.getInt(ID_COL_NUM));
			theme.setName(cursor.getString(NAME_COL_NUM));
			theme.setLevelId(cursor.getInt(LEVEL_COL_NUM));
			themes.add(theme);
		} while (cursor.moveToNext());
		cursor.close();

		return themes;
	}
	
	/**
	 * Gets themes of the database according their level.
	 * 
	 * @return the list of theme.
	 */
	public List<Theme> getThemesWithLevel(long levelId) {
		Cursor cursor = this.db.query(
				THEME_TABLE,
				new String[] { ID, NAME, LEVEL_ID },
				LEVEL_ID + " = " + levelId,
				null, null, null, null
		);

		return this.cursorToThemes(cursor);
	}
	
	private Theme cursorToTheme(Cursor cursor) {
		if(cursor.getCount() == 0) {
			return null;
		} else {
			cursor.moveToFirst();
		}
		
		Theme theme = new Theme();
		theme.setId(cursor.getInt(ID_COL_NUM));
		theme.setName(cursor.getString(NAME_COL_NUM));
		theme.setLevelId(cursor.getInt(LEVEL_COL_NUM));
		cursor.close();
		
		return theme;
	}
	
	/**
	 * Gets the theme object according his name and his level id.
	 * 
	 * @param name the theme name.
	 * @param levelId the theme level id.
	 * @return the theme object.
	 */
	public Theme getThemeWithNameAndLevel(String name, long levelId) {
		Cursor cursor = this.db.query(
				THEME_TABLE,
				new String[] { ID, NAME, LEVEL_ID },
				NAME + " LIKE \"" + name + "\" and " + LEVEL_ID + " = " + levelId,
				null, null, null, null
		);
		
		return this.cursorToTheme(cursor);
	}
}
